package com.yisingle.study.map.one.utils;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.amap.api.services.core.LatLonPoint;
import com.yisingle.study.map.one.data.CarPositionData;

import java.util.List;

/**
 * @author jikun
 *         Created by jikun on 2018/3/14.
 */

public class DelayTest {
    public OnCallBack callBack;


    private LatLonPoint latLonPoint;


    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (null != callBack) {
                    callBack.onFailed();
                }
            } else if (msg.what == 1) {
                if (null != callBack) {
                    List<CarPositionData> list = NearbyCarUtils.getNearByCar(latLonPoint.getLatitude(), latLonPoint.getLongitude());
                    callBack.onSuccess(list);
                }
            }

        }
    };

    public void getDelayPost(LatLonPoint latLonPoint, OnCallBack callBack) {
        this.callBack = callBack;
        this.latLonPoint = latLonPoint;
        long sec = (long) (Math.random() * 1300);
        Log.e("测试代码", "测试代码时间=" + sec);
        if (sec > 1000) {
            handler.sendEmptyMessageDelayed(0, sec);
        } else {
            handler.sendEmptyMessageDelayed(1, sec);
        }

    }

    public void stop() {
        this.callBack = null;
    }

    public interface OnCallBack {
        void onSuccess(List<CarPositionData> list);

        void onFailed();
    }

    public void destory() {
        handler.removeCallbacksAndMessages(null);
    }
}
